package com.supcon.exchangedata.service.lq;

import com.supcon.exchangedata.pojo.entity.Gps.Gps;
import com.supcon.exchangedata.pojo.entity.oracle.CtGpsStatisitcInfo;
import com.supcon.exchangedata.pojo.entity.mysql.operation.lq.LqMaintainCarLocus;
import com.supcon.exchangedata.pojo.entity.oracle.CtGpsStatisitcInfoHis;
import com.supcon.exchangedata.repository.master.SlaveCarGpsInfo;
import com.supcon.exchangedata.repository.master.SlaveCarGpsInfoHis;
import com.supcon.exchangedata.repository.slave7.SlaveLqCarGpsInfo;
import com.supcon.exchangedata.utils.PositionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/12/23/16:07
 * @Description:
 */
@Service
@Slf4j
public class LqCarGpsService {
    @Resource
    private SlaveLqCarGpsInfo slaveLqCarGpsInfo;
    @Resource
    private SlaveCarGpsInfo slaveCarGpsInfo;
    @Resource
    private SlaveCarGpsInfoHis slaveCarGpsInfoHis;


    public void syncLqCarGps() {
        long startTime = System.currentTimeMillis();
        List<CtGpsStatisitcInfo> list = new ArrayList<>();
        List<LqMaintainCarLocus> all = slaveLqCarGpsInfo.findGpsCar();
        if (!CollectionUtils.isEmpty(all)) {
            try {
                slaveCarGpsInfo.deleteByTypeAndLayerCodeDiy(5L, "5015");
            }catch (Exception e){

            }

            DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            for (LqMaintainCarLocus hjCleanGps : all) {
                LocalDateTime bizTime = hjCleanGps.getBizTime();
                String times = timeFormatter.format(bizTime);
                CtGpsStatisitcInfo gpsStatisitcInfo = new CtGpsStatisitcInfo();
                String carNumber = hjCleanGps.getCarNumber();
                gpsStatisitcInfo.setLicensePlateNum(carNumber);
                gpsStatisitcInfo.setType(5L);
                gpsStatisitcInfo.setCurrTime(times);
                gpsStatisitcInfo.setLayerCode("5015");
                BigDecimal oriLatitude = hjCleanGps.getLatitude();
                BigDecimal oriLongitude = hjCleanGps.getLongitude();
                if (hjCleanGps.getLatitude() != null && hjCleanGps.getLatitude().compareTo(BigDecimal.ZERO)>0 &&
                        hjCleanGps.getLongitude() != null && hjCleanGps.getLongitude().compareTo(BigDecimal.ZERO)>0) {
                    Gps gps = PositionUtil.gcj_To_Gps84(hjCleanGps.getLongitude().doubleValue(), hjCleanGps.getLatitude().doubleValue());
                    gpsStatisitcInfo.setLatitude(gps.getWgLat());
                    gpsStatisitcInfo.setLongitude(gps.getWgLon());
                    list.add(gpsStatisitcInfo);

                }
            }
        }
        if (!CollectionUtils.isEmpty(list)) {
            slaveCarGpsInfo.saveAll(list);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("一共耗时:" + (endTime - startTime) / 1000 + "秒");
    }
    public void newSyncLqCarGps(){
        List<LqMaintainCarLocus> all = slaveLqCarGpsInfo.findAll();
        all.forEach(v->{
            Long id = v.getId();
            CtGpsStatisitcInfoHis one = slaveCarGpsInfoHis.getOne(id.toString());

        });
    }
}
